
package com.xiaoma.myaudience.util.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

/**
 * 信息bean 基础类. <br/>
 * 继承BaseColumns
 * 
 * @author wjying
 */
public class BaseColumnInfo extends BaseInfo implements BaseColumns {
    public long _id = -1;

    public int _count = -1;

    /**
     * 子类覆盖此方法,实现从Cursor中读入 <br/>
     * 注意使用时Cursor应已经在正确位置
     * 
     * @param cursor
     */
    public void readFromCursor(Cursor cursor) {
        int index = -1;
        if ((index = cursor.getColumnIndex(_ID)) != -1) {
            _id = cursor.getLong(index);
            index = -1;
        }
        if ((index = cursor.getColumnIndex(_COUNT)) != -1) {
            _count = cursor.getInt(index);
            index = -1;
        }
    }

    /**
     * 子类覆盖此方法,生成一个 ContentValues<br/>
     * 此方法产生的ContentValues不包含_ID
     * 
     * @return
     */
    public ContentValues generateContentValues() {
        ContentValues values = new ContentValues();
        return values;
    }

    @Override
    public void readFromJSONObject(JSONObject o) throws JSONException {
        super.readFromJSONObject(o);
        if (o != null) {
            if (o.has(_ID))
                _id = o.getLong(_ID);
            if (o.has(_COUNT))
                _count = o.getInt(_COUNT);
        }
    }

    @Override
    public void readFromParcel(Parcel source) {
        super.readFromParcel(source);
        _id = source.readLong();
        _count = source.readInt();
    }

    @Override
    public void writeToParcel(Parcel parcel, int paramInt) {
        parcel.writeLong(_id);
        parcel.writeInt(_count);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<BaseColumnInfo> CREATOR = new Creator<BaseColumnInfo>() {
        public BaseColumnInfo createFromParcel(Parcel source) {
            BaseColumnInfo info = new BaseColumnInfo();
            info.readFromParcel(source);
            return info;
        }

        public BaseColumnInfo[] newArray(int size) {
            return new BaseColumnInfo[size];
        }
    };
}
